package com.briup.test;

/**
 * @author 甜圆
 * @version v1.0.1
 * @date 2025/9/25 11:20
 * @package com.briup.test
 * @copyright 甜圆
 */

public class ScoreBoard {
    // 用volatile修饰积分变量
    private volatile int score = 0;

    // 加分方法
    public void addScore() {
        // 每次加1分
        score++;
        System.out.println("加分后: " + score);
    }

    // 查看当前分数
    public int getScore() {
        return score;
    }

    public static void main(String[] args) {
        ScoreBoard board = new ScoreBoard();

        // 两个同学同时加分
        Thread studentA = new Thread(() -> {
            for (int i = 0; i < 3; i++) {
                board.addScore();
            }
        });

        Thread studentB = new Thread(() -> {
            for (int i = 0; i < 3; i++) {
                board.addScore();
            }
        });

        studentB.start();
        studentA.start();
    }
}